package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjFactoryStrategy;
import com.zb.mapper.ZjFactoryStrategyMapper;
import com.zb.service.ZjFactoryStrategyService;
import com.zb.vo.FactoryStrategyVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 厂商策略表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-27
 */
@Service
public class ZjFactoryStrategyServiceImpl extends ServiceImpl<ZjFactoryStrategyMapper, ZjFactoryStrategy> implements ZjFactoryStrategyService {
    @Resource
    private ZjFactoryStrategyMapper factoryStrategyMapper;

    // 保存或更新品牌商家策略
    @Override
    public void save(Long factoryId, FactoryStrategyVo strategy) {
        ZjFactoryStrategy factoryStrategy = new ZjFactoryStrategy();
        BeanUtil.copyProperties(strategy, factoryStrategy);
        factoryStrategy.setId(factoryId);
        this.saveOrUpdate(factoryStrategy);
    }

    // 根据品牌商家ID获取结算类型
    @Override
    public Integer getSettlementType(Long factoryId) {
        Integer settlementType = factoryStrategyMapper.getSettlementTypeById(factoryId);
        return null == settlementType? 1 : settlementType;
    }

    // 批量更新开启或关闭工单配件权限
    @Override
    public void updateEnableSpareAuthoriBatchById(List<Long> ids, boolean enableSpareAuthori) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        factoryStrategyMapper.updateEnableSpareAuthoriBatchById(ids, enableSpareAuthori);
    }

    // 批量更新开启或关闭申请工单配件
    @Override
    public void updateEnableApplySpareBatchById(List<Long> ids, boolean enableSpare) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        factoryStrategyMapper.updateEnableApplySpareBatchById(ids, enableSpare);
    }

    // 批量更新开启或关闭审核工单配件
    @Override
    public void updateEnableCheckSpareBatchById(List<Long> ids, boolean enableCheckSpare) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        factoryStrategyMapper.updateEnableCheckSpareBatchById(ids, enableCheckSpare);
    }

    // 批量更新开启或关闭工单费用权限
    @Override
    public void updateEnableFeeAuthoriBatchById(List<Long> ids, boolean enableFeeAuthori) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        factoryStrategyMapper.updateEnableFeeAuthoriBatchById(ids, enableFeeAuthori);
    }

    // 批量更新开启或关闭申请工单费用
    @Override
    public void updateEnableApplyFeeBatchById(List<Long> ids, boolean enableFee) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        factoryStrategyMapper.updateEnableApplyFeeBatchById(ids, enableFee);
    }

    // 批量更新开启或关闭审核工单费用
    @Override
    public void updateEnableCheckFeeBatchById(List<Long> ids, boolean enableCheckFee) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        factoryStrategyMapper.updateEnableCheckFeeBatchById(ids, enableCheckFee);
    }

    // 批量更新结算类型
    @Override
    public void updateSettlementTypeBatchById(List<Long> ids, Integer settlementType) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        factoryStrategyMapper.updateSettlementTypeBatchById(ids, settlementType);
    }

    // 获取厂商策略
    @Override
    public List<ZjFactoryStrategy> getByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjFactoryStrategy> wrapper = new QueryWrapper<>();
        wrapper.select("id", "settlement_type")
                .in("id", ids);
        return this.list(wrapper);
    }
}
